//
//  RX.m
//  iMed

// An RX is a prescription written for an NDCitem

//
//  Created by Barry Press on 8/14/09.
//  Copyright 2009 Barry Press. All rights reserved.
//

#import "RX.h"
#import "Constants.h"


@implementation RX

#pragma mark Archiving methods

// ---------------------------------------------- dealloc ----------------------
- (void)dealloc
{
	[written release];
	written = NULL;
	[filled release];
	filled = NULL;
	[ndc release];
	ndc = NULL;
	[super dealloc];
}

// ---------------------------------------------- description ------------------
- (NSString*)description
{
	NSAssert(written != NULL, @"Missing written date in RX");
	NSAssert(filled != NULL, @"Missing filled date in RX");
	NSAssert(ndc != NULL, @"Missing NDC in RX");

	NSString *s = [NSString stringWithFormat:@"Written:%@, Filled:%@\nNDC:%@\nDose:%.1f, Interval:%.1f hours, Perday:%.1f", 
				   [written descriptionWithCalendarFormat:@"%M/%D/%Y" timeZone:nil locale:nil], 
				   [filled descriptionWithCalendarFormat:@"%M/%D/%Y" timeZone:nil locale:nil], 
				   ndc, dose, interval / HOUR, perday];
	return s;
}

// ---------------------------------------------- encodeWithCoder --------------
- (void)encodeWithCoder:(NSCoder *)coder
{
	[coder encodeObject:written forKey:@"RXwritten"];
	[coder encodeObject:filled forKey:@"RXfilled"];
	[coder encodeObject:ndc forKey:@"RXNDC"];
	[coder encodeFloat:dose forKey:@"RXdose"];
	[coder encodeDouble:interval forKey:@"RXinterval"];
	[coder encodeFloat:perday forKey:@"RXperday"];
	[coder encodeBool:bAuto forKey:@"RXbAuto"];
}

// ---------------------------------------------- init -------------------------
- (id)init
{
	[super init];
	written = nil;
	filled = nil;
	ndc = nil;
	dose = 0.0;
	interval = 0.0;
	perday = 0.0;
	bAuto = YES;
	return self;
}

// ---------------------------------------------- initWithCoder ----------------
- (id)initWithCoder:(NSCoder *)coder
{
	[self init];
	[self setWritten:[coder decodeObjectForKey:@"RXwritten"]];
	[self setFilled:[coder decodeObjectForKey:@"RXfilled"]];
	[self setNdc:[coder decodeObjectForKey:@"RXNDC"]];
	[self setDose:[coder decodeFloatForKey:@"RXdose"]];
	[self setInterval:[coder decodeDoubleForKey:@"RXinterval"]];
	[self setPerday:[coder decodeFloatForKey:@"RXperday"]];
	[self setBAuto:[coder decodeBoolForKey:@"RXbAuto"]];
	return self;
}

// ---------------------------------------------- initWithDateWritten:dateFilled:Ndc:Dose:Perday:
- (id)initWithDateWritten:(NSDate*)dWritten dateFilled:(NSDate*)dFilled Ndc:(NDCitem*)anNdc 
					 Dose:(float)aDose Perday:(float)aPerday
{
	[self init];
	[self setWritten:dWritten];
	[self setFilled:dFilled];
	[self setNdc:anNdc];
	[self setDose:aDose];
	[self setPerday:aPerday];
	return self;
}

// ---------------------------------------------- initWithDateWritten:dateFilled:Ndc:Dose:Interval:
- (id)initWithDateWritten:(NSDate*)dWritten dateFilled:(NSDate*)dFilled Ndc:(NDCitem*)anNdc 
					 Dose:(float)aDose Interval:(double)anInterval 
{
	[self init];
	[self setWritten:dWritten];
	[self setFilled:dFilled];
	[self setNdc:anNdc];
	[self setDose:aDose];
	[self setInterval:anInterval];
	return self;
}

#pragma mark interval and perday

// ---------------------------------------------- interval and perday ----------
// ---------------------------------------------- interval ---------------------
- (NSTimeInterval)interval
{
	return interval;
}

- (void)setInterval:(NSTimeInterval)newInterval
{
	[self willChangeValueForKey:@"perday"];
	interval = newInterval;
	perday = ONEDAY / interval;
	[self didChangeValueForKey:@"perday"];
}

// ---------------------------------------------- perday -----------------------
- (float)perday
{
	return perday;
}

- (void)setPerday:(float)newPerday
{
	[self willChangeValueForKey:@"interval"];
	 perday = newPerday;
	 interval = ONEDAY / perday;
	[self didChangeValueForKey:@"interval"];
}

#pragma mark Accessor methods

// ---------------------------------------------- accessors --------------------
@synthesize written;
@synthesize filled;
@synthesize ndc;
@synthesize dose;
@synthesize bAuto;

@end
